package xian.woniuxy.customer.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.mybatis.core.page.PageQuery;
import com.ruoyi.common.mybatis.core.page.TableDataInfo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;
import xian.woniuxy.customer.domain.bo.PrescriptionBo;
import xian.woniuxy.customer.domain.vo.PrescriptionVo;
import xian.woniuxy.customer.dubboService.RemoteEnterService;
import xian.woniuxy.customer.mapper.PrescriptionMapper;
import xian.woniuxy.customer.service.IPrescriptionService;

import xian.woniuxy.domain.Enter;
import xian.woniuxy.domain.Inspect;
import xian.woniuxy.domain.InspectOrder;
import xian.woniuxy.domain.Prescription;
import xian.woniuxy.reghos.dubboService.RemoteInspectOrderService;
import xian.woniuxy.reghos.dubboService.RemoteInspectService;


import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 处方Service业务层处理
 *
 * @author ataoxz
 * @date 2022-08-23
 */
@RequiredArgsConstructor
@Service
public class PrescriptionServiceImpl implements IPrescriptionService {

    private final PrescriptionMapper baseMapper;

    @DubboReference
    private RemoteInspectService remoteInspectService;

    @DubboReference
    private RemoteInspectOrderService remoteInspectOrderService;

    @DubboReference
    private RemoteEnterService remoteEnterService;

    /**
     * 查询处方
     */
    @Override
    public PrescriptionVo queryById(Long prescriptionId) {
        return baseMapper.selectVoById(prescriptionId);
    }

    /**
     * 查询处方列表
     */
    @Override
    public TableDataInfo<PrescriptionVo> queryPageList(PrescriptionBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<Prescription> lqw = buildQueryWrapper(bo);
        Page<PrescriptionVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询处方列表
     */
    @Override
    public List<PrescriptionVo> queryList(PrescriptionBo bo) {
        LambdaQueryWrapper<Prescription> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<Prescription> buildQueryWrapper(PrescriptionBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<Prescription> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getPatientId() != null, Prescription::getPatientId, bo.getPatientId());
        lqw.like(StringUtils.isNotBlank(bo.getPatientName()), Prescription::getPatientName, bo.getPatientName());
        lqw.eq(bo.getRegisterId() != null, Prescription::getRegisterId, bo.getRegisterId());
        lqw.eq(bo.getRecordId() != null, Prescription::getRecordId, bo.getRecordId());
        lqw.eq(bo.getDepId() != null, Prescription::getDepId, bo.getDepId());
        lqw.like(StringUtils.isNotBlank(bo.getDepName()), Prescription::getDepName, bo.getDepName());
        lqw.eq(bo.getPrescriptionDate() != null, Prescription::getPrescriptionDate, bo.getPrescriptionDate());
        lqw.eq(bo.getUserId() != null, Prescription::getUserId, bo.getUserId());
        lqw.like(StringUtils.isNotBlank(bo.getUserName()), Prescription::getUserName, bo.getUserName());
        lqw.eq(StringUtils.isNotBlank(bo.getRpMedicine()), Prescription::getRpMedicine, bo.getRpMedicine());
        lqw.eq(StringUtils.isNotBlank(bo.getRpInspect()), Prescription::getRpInspect, bo.getRpInspect());
        lqw.eq(StringUtils.isNotBlank(bo.getDiagnosis()), Prescription::getDiagnosis, bo.getDiagnosis());
        return lqw;
    }

    /**
     * 新增处方
     */
    @Override
    public Boolean insertByBo(PrescriptionBo bo) {
        Prescription add = BeanUtil.toBean(bo, Prescription.class);
        add.setPrescriptionDate(new Date());
        validEntityBeforeSave(add);
//        final String InspectName = add.getRpInspect();
//        if (InspectName != null && InspectName != "") {
//            //根据是否有检查项目名称查询检查项目表获取检查项目实例
//            final Inspect inspect = remoteInspectService.queryByInspectName(InspectName);
//            final Long inspectId = inspect.getInspectId();
//            //检查项目订单表实例
//            final InspectOrder inspectOrder = new InspectOrder();
//            //生成检查项目订单表
//            inspectOrder.setInspectId(inspectId);
//            inspectOrder.setInspectOrderDate(new Date());
//            inspectOrder.setPatientId(add.getPatientId());
//            inspectOrder.setUserId(add.getUserId());
//            inspectOrder.setRegisterId(add.getRegisterId());
//            //判断病人是否住院
//            final Enter enter = remoteEnterService.queryByPatientId(add.getPatientId());
//            if (enter != null) {
//                inspectOrder.setEnterId(enter.getEnterId());
//            }
//            inspectOrder.setInspectOrderPrice(inspect.getInspectPrice());
//            //添加检查项目订单表
//            remoteInspectOrderService.inster(inspectOrder);
//        }
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setPrescriptionId(add.getPrescriptionId());
        }
        return flag;
    }

    /**
     * 修改处方
     */
    @Override
    public Boolean updateByBo(PrescriptionBo bo) {
        Prescription update = BeanUtil.toBean(bo, Prescription.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(Prescription entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除处方
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
